package com.wgq.uitest.exp7;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.wgq.uitest.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = "/app/Ambient")
public class AmbientActivity extends AppCompatActivity implements SensorEventListener {

    @BindView(R.id.Lc_ambient_temp)
    LineChart lc_ambient_temp;
    private int timeIndex = 0;
    SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ambient);

        ButterKnife.bind(this);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        lc_ambient_temp.setTouchEnabled(false);
        lc_ambient_temp.getXAxis().setEnabled(true);
        lc_ambient_temp.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lc_ambient_temp.getAxisLeft().setEnabled(true);
        lc_ambient_temp.getAxisRight().setEnabled(false);
        lc_ambient_temp.getAxisLeft().setAxisMaximum(50);
        lc_ambient_temp.getAxisLeft().setAxisMinimum(-20);
        List<Entry> TempEntries = new ArrayList<>();
        LineDataSet TempDataSet = new LineDataSet(TempEntries, "TEMP");
        TempDataSet.setColor(Color.rgb(255,0,0));
        ((LineDataSet) TempDataSet).setDrawCircles(false);
        LineData lineData = new LineData();
        lineData.addDataSet(TempDataSet);
        lc_ambient_temp.setData(lineData);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE), SensorManager.SENSOR_DELAY_NORMAL);
    }


    @Override
    protected void onStop() {
        super.onStop();
        sensorManager.unregisterListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        LineData lineData = lc_ambient_temp.getData();
        if(lineData != null){
            LineDataSet tempDataSet = (LineDataSet) lineData.getDataSetByLabel("TEMP", true);
            tempDataSet.addEntry(new Entry(timeIndex++, values[0]));
            if(tempDataSet.getEntryCount() < 200){
                tempDataSet.addEntry(new Entry(timeIndex++, values[0]));
                lc_ambient_temp.getXAxis().setAxisMaximum(200);
            }
            else{
                tempDataSet.removeFirst();
                lc_ambient_temp.getXAxis().setAxisMaximum(tempDataSet.getXMax());
            }
            lc_ambient_temp.getAxisLeft().setAxisMinimum(tempDataSet.getYMin());
            lc_ambient_temp.getAxisLeft().setAxisMaximum(tempDataSet.getYMax());
            lc_ambient_temp.getXAxis().setAxisMinimum(tempDataSet.getXMin());
            lc_ambient_temp.notifyDataSetChanged();
            lc_ambient_temp.invalidate();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}